class request {
    constructor() {
        this._baseUrl = 'http://127.0.0.1:8090/';
        //this._baseUrl = 'http://124.70.202.238:8090/';
        this._token = wx.getStorageSync('token');
        //  this._header = {'Authorization': 'Bearer ' + token}
        this._header = {};
    }

    /**
     * GET类型的网络请求
     */
    getRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'GET')
    }

    /**
     * DELETE类型的网络请求
     */
    deleteRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'DELETE')
    }

    /**
     * PUT类型的网络请求
     */
    putRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'PUT')
    }

    /**
     * POST类型的网络请求
     */
    postRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'POST')
    }

    /**
     * 网络请求
     */
    requestAll(url, data, header, method) {
        return new Promise((resolve, reject) => {
            wx.request({
                url: this._baseUrl + url,
                data: data,
                header: header,
                method: method,
                success: (res => {
                    if (res.statusCode === 200) {
                        //200: 服务端业务处理正常结束
                        resolve(res)
                    } else {
                        //其它错误，提示用户错误信息
                        reject(res)
                    }
                }),
                fail: (res => {
                    reject(res)
                })
            })
        })
    }
}

export default request